﻿/// <reference path="~/Scripts/FlowBasis/FlowBasis-all-vsdoc.js" />

FlowBasis.defineNamespace("Rpc");


FlowBasis.Rpc.invoke = function (url, requestMessage, callback) {
    /// <summary>
    /// Callback should be of format function(object response, bool success)
    /// </summary>

    url = FlowBasis.resolveUrl(url);

    jQuery.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(requestMessage),
        dataType: "json",
        processData: false,

        success: function (data, textStatus, XMLHttpRequest) {
            if (data != null) {
                if (data.error != null) {
                    callback(data, false);
                }
                else {
                    callback(data, true);
                }
            }
            else {
                if (XMLHttpRequest.status == 0) {
                    var response = {
                        error: {
                            message: "Request was canceled.",
                            canceled: true
                        }
                    };
                }
                else {
                    callback(null, true);
                }
            }
        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var response = {
                error: {
                    message: "Request failed.",
                    http: {
                        status: XMLHttpRequest.status,
                        responseText: XMLHttpRequest.responseText,
                        contentType: XMLHttpRequest.getResponseHeader("Content-Type")
                    }
                }
            };

            callback(response, false);
        }
    });
};



FlowBasis.Rpc.invokeAndWait = function (url, requestMessage, callback, suppressDefaultErrorMessage) {
    /// <summary>
    /// Callback should be of format function(object response, bool success).
    /// Set suppressDefaultErrorMessage to true to disable auto-showing of error dialog.
    /// </summary>

    url = FlowBasis.resolveUrl(url);

    var self = this;
    self.showWaitingDialog();

    self.invoke(url, requestMessage,
        function (response, success) {
            self.hideWaitingDialog();

            if (success) {
                if (callback) {
                    callback(response, success);
                }
            }
            else {
                if (!suppressDefaultErrorMessage) {
                    self.showErrorDialog(response.error);
                }

                if (callback) {
                    callback(response, success);
                }
            }
        });
};



FlowBasis.Rpc.errorDialogElement = null;
FlowBasis.Rpc.showErrorDialog = function (errorObject) {

    if (this.errorDialogElement == null) {
        this.errorDialogElement = document.createElement("div");
        this.errorDialogElement.style.display = "none";
        document.body.appendChild(this.errorDialogElement);

        jQuery(this.errorDialogElement).dialog({
            modal: true,
            autoOpen: false,
            title: "Error",
            width: 500,
            height: 300,
            maxWidth: 750,
            maxHeight: 600
        });
    }

    this.errorDialogElement.innerHTML = "";

    if (!errorObject.http) {
        this.errorDialogElement.appendChild(document.createTextNode(errorObject.message));
    }
    else {
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "about:blank");
        iframe.setAttribute("frameborder", "0");
        iframe.setAttribute("marginwidth", "0");
        iframe.setAttribute("marginheight", "0");
        iframe.style.width = '100%';
        iframe.style.height = '95%';
        iframe.style.margin = '0px';
        iframe.style.padding = '0px';
        iframe.onload = function () {
            iframe.contentDocument.body.innerHTML = errorObject.http.responseText;
        };
        this.errorDialogElement.appendChild(iframe);
    }

    jQuery(this.errorDialogElement).dialog("open");
};


FlowBasis.Rpc.waitDialogElement = null;
FlowBasis.Rpc.showWaitingDialog = function () {

    if (this.waitDialogElement == null) {
        this.waitDialogElement = document.createElement("div");
        this.waitDialogElement.style.display = "none";
        this.waitDialogElement.innerHTML = "Waiting...";
        document.body.appendChild(this.waitDialogElement);

        jQuery(this.waitDialogElement).dialog({
            modal: true,
            draggable: false,
            autoOpen: false,
            title: "Waiting",
            closeOnEscape: false,
            resizable: false
        });
    }

    jQuery(this.waitDialogElement).dialog("open");
};

FlowBasis.Rpc.hideWaitingDialog = function () {
    jQuery(this.waitDialogElement).dialog("close");
};